package web.config;

import web.common.BaseModifier;
import web.common.CodeEngineConfig;
import web.common.XmlAccessUtil;

import org.dom4j.*;

/**
 * Created by IntelliJ IDEA.
 * User: duanbin
 * Date: 2007-8-22
 * Time: 11:11:07
 * To change this template use File | Settings | File Templates.
 */
public class ManagerBeanModifier extends BaseModifier {
    public String getFileName() {
        return CodeEngineConfig.getSpringManagerFile();
    }

    public void doModify(String name) {
        //NOTES: hardcode using 'ns' as default namespace
        String manager = CodeEngineConfig.getFisrtControllerManager(name);
        if(manager!=null){
            getTargetNode(manager).attribute("class").setValue(
                CodeEngineConfig.getManagerPackage() + ".impl." +
                        manager + "Impl");
        }

    }
    private Element getTargetNode(String dist){
        Element distNode = (Element) getXPath4DefaultNS("//ns:bean[@id='"+ uncapFirst(dist) +"']").selectSingleNode(doc);
        if(distNode==null){
            Element commonNode = (Element) getXPath4DefaultNS("//ns:bean[@id='commonMgr']").selectSingleNode(doc);
            distNode = commonNode.createCopy();
            XmlAccessUtil.insertElementAfter(commonNode, distNode);
            distNode.attribute("id").setValue(uncapFirst(dist));
        }
        return distNode;
    }
}
